<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 列表管理
 */
namespace app\content\model;

use app\content\model\Model;
use Mscore\Core\Lib\Upload;

class Article extends Model{
    
    private $article = 'sys_article';
    private $articleclass = 'sys_articleclass';

    /**
     * 内容類別列表
     * @param array $catelist 
     * @param array $where 
     */ 
    public function catelist($where=null){
        //類別列表
        $catelist = Model::name($this->articleclass)
            ->where($where)
            ->order('id asc')
            ->select();
        return $catelist; 
    }

    /**
     * 文章類別添加
     * @param array $cateinfo 
     * @param array $where 
     */ 
    public function cateinfo($id=null,$field='*'){
        $info =  Model::name($this->articleclass)
          ->field($field)
          ->where(['id'=>$id])
          ->find();
        $info['langinfo']=[];
        if(isset($info['introduction'])&&!empty($info['titles'])){
            $info['introduction']=json_decode($info['introduction'],true);  
        }
        if(isset($info['titles'])&&!empty($info['titles'])){
            $info['titles']=json_decode($info['titles'],true);
            foreach ($info['titles'] as $key => $value) {
                 $info['langinfo'][$key]['titles']=$value;
                 $info['langinfo'][$key]['introduction']=isset($info['introduction'][$key])?$info['introduction'][$key]:'';
             } 
             
        }
        unset($info['introduction'],$info['titles']);
        // print_r($info);die;
        return $info;

    }

    /**
     * 類別保存
     * @param array $catesave 
     * @param array $where 
    */ 
    public function catesave($data){
        //获取封面图片
        $categoryimg=[];
        if(isset($data['id'])&&!empty($data['id'])){
            $categoryimg = $this->cateinfo($data['id'],'imgurl,mimgurl');
        }
        switch (true) {
            case isset($data['imgOne1'])&&!empty($data['imgOne1']):
                $url="/static/upload/category/";
                $imgurl = (new Upload)->Uploadbase64($data['imgOne1'],$url,time());
                if(isset($categoryimg['imgurl'])&&file_exists(config('file.path') . $categoryimg['imgurl'])){
                    @unlink (config('file.path') . $categoryimg['imgurl']); 
                }
                break;
            default:
                $imgurl=isset($categoryimg['imgurl'])?$categoryimg['imgurl']:'';
                break;
        }
        switch (true) {
            case isset($data['imgOne2'])&&!empty($data['imgOne2']):
                $url="/static/upload/category/";
                $mimgurl = (new Upload)->Uploadbase64($data['imgOne2'],$url,'i'.time());
                if(isset($categoryimg['mimgurl'])&&file_exists(config('file.path') . $categoryimg['mimgurl'])){
                    @unlink (config('file.path') . $categoryimg['mimgurl']); 
                }
                break;
            default:
                $mimgurl=isset($categoryimg['mimgurl'])?$categoryimg['mimgurl']:'';
                break;
        }
        $titles=[];
        $introduction=[];
        $content=[];
        if(isset($data['langs'])&&!empty($data['langs'])){
            foreach ($data['langs'] as $key => $value) {
                $titles[$value]=$data['titles'][$key];
            }
            foreach ($data['langs'] as $key => $value) {
                $introduction[$value]=$data['introduction'][$key];
            }
        };
        $data  = [
            'id'=>$data['id'],
            'title'=>$data['title'],
            'titles'=>json_encode($titles,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'introduction' =>json_encode($introduction,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'pid'=>$data['pid'],
            'acode'=>$data['acode'],
            'pagecode'=>!empty($data['pagecode'])?$data['pagecode']:$data['acode'],
            'imgurl'=>$imgurl,
            'mimgurl'=>$mimgurl,
            'status'=>$data['status'],
            'orderid'=>$data['orderid'],
            'url'=>$data['url'],
            'is_index'=>$data['is_index'],
            'is_target'=>$data['is_target']
        ];
        switch (true) {
            case isset($data['id'])&&!empty($data['id']):
                $id=$data['id']; unset($data['id']);
                Model::name($this->articleclass)->where('id=' . $id)->update($data);
                return true;
                break;
            
            default:
                unset($data['id']);
                Model::name($this->articleclass)->insert($data);
                return true;
                break;
        }
        return false;
    }

     /**
     * 類別删除
     * @param array $catedel 
     * @param array $where 
     */
    public function catedel($id){
         //删除
        if ($id) {
            Model::name($this->articleclass)->where('id='.$id)->delete();
            return true;
        }
        return false;
    }

    /**
     * 内容列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list($where=null,$page=1,$page_size=10){
        //内容列表
        $article['datalist'] = Model::name($this->article)
            ->alias('a')
            ->join($this->articleclass.' b','a.cid=b.id','left')
            ->field('a.*, b.title as cname')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('id desc')
            ->select();
        //数据总数
        $article['total']=Model::name($this->article)
            ->alias('a')
            ->where($where)
            ->count('id');
        return $article;
    }

    /**
     * 内容添加
     * @param array $info 
     * @param array $where 
     */ 
    public function info($id=null,$field='*'){  
        $info = Model::name($this->article)
           ->field($field)
           ->where(['id'=>$id])
           ->find();;
        $info['langinfo']=[];
        if(isset($info['introduction'])&&!empty($info['introduction'])){
            $info['introduction']=json_decode($info['introduction'],true);  
        }
        if(isset($info['content'])&&!empty($info['content'])){
            $info['content']=json_decode($info['content'],true);  
        }
        if(isset($info['titles'])&&!empty($info['titles'])){
            $info['titles']=json_decode($info['titles'],true);
            $i=0;
            foreach ($info['titles'] as $key => $value) {
                 $info['langinfo'][$key]['titles']=$value;
                 $info['langinfo'][$key]['introduction']=isset($info['introduction'][$key])?$info['introduction'][$key]:'';
                 $info['langinfo'][$key]['content']=isset($info['content'][$key])?$info['content'][$key]:'';
                 $info['langinfo'][$key]['id']=$i;
                 $i++;
             }    
        }
        unset($info['introduction'],$info['content'],$info['titles']);
        return $info;
    }

    /**
     * 内容保存
     * @param array $articlesave 
     * @param array $where 
     */ 
    public function save($data,$id=null){
        
        //获取封面图片
        $img=[];
        if(isset($data['id'])&&!empty($data['id'])){
            $img = $this->info($data['id'],'imgurl');
        }
        $time=time();
        switch (true) {
            case isset($data['imgOne'])&&!empty($data['imgOne']):
                $url="/static/upload/article/";
                $imgurl = (new Upload)->Uploadbase64($_POST['imgOne'],$url,$time);
                if(isset($img['imgurl'])&&file_exists(config('file.path') . $img['imgurl'])){
                    @unlink (config('file.path') . $img['imgurl']); 
                }
                break;
            default:
                $imgurl=isset($img['imgurl'])?$img['imgurl']:'';
                break;
        }
        $titles=[];
        $introduction=[];
        $content=[];
        if(isset($data['langs'])&&!empty($data['langs'])){
            foreach ($data['langs'] as $key => $value) {
                $titles[$value]=$data['titles'][$key];
            }
            foreach ($data['langs'] as $key => $value) {
                $introduction[$value]=$data['introduction'][$key];
            }
            foreach ($data['langs'] as $key => $value) {
                $content[$value]=htmlspecialchars($data['content'][$key]);
            }
        };

        $data  = [
            'id'=>$data['id'],
            'cid'=>$data['cid'],
            'title'=>$data['title'],
            'titles'=>json_encode($titles,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'introduction' =>json_encode($introduction,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'keywords'=>$data['keywords'],
            'description'=>$data['description'],
            'imgurl'=>$imgurl,
            'content' =>json_encode($content,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
            'is_open'=>$data['is_open'],
            'open_url'=>$data['open_url'],
            'status'=>$data['status'],
            'author'=>$data['author'],
            'is_index'=>$data['is_index'],
            'createtime'=>$data['createtime']
        ];
        if(isset($data['cid'])&&!empty($data['cid'])){
            $cata = Model::name($this->articleclass)->where(['id'=>$data['cid']])->field('acode,titles,pagecode')->find();
            $data['acode']=$cata['acode'];
            $data['pagecode']=$cata['pagecode'];
            $data['cname']=$cata['titles'];
        }
        switch (true) {
            case isset($data['id'])&&!empty($data['id']):
                $id=$data['id']; unset($data['id']);
                Model::name($this->article)->where('id=' . $id)->update($data);
                return true;
                break;
            
            default:
                unset($data['id']);
                Model::name($this->article)->insert($data);
                return true;
                break;
        }
        return false;
    }

    /**
     * 内容删除
     * @param array $del 
     * @param array $where 
     */
    public function del($id=null){
     //删除
        if ($id) {
            Model::name($this->article)->where('id='.$id)->delete();
            return true;
        }
        return false;
    }

}
